﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace de.codingfreaks.libs.cfThreadingTools.Extensions
{
    /// <summary>
    /// Provides extensions for <c>System.Windows.Forms.ListView and subsequent objects.</c>
    /// </summary>
    public static class ListViewExtensions
    {

        #region extensions for ListView

        /// <summary>
        /// Thread-safe way to add an item to a <c>ListView</c>.
        /// </summary>
        /// <param name="lvwControl">The <c>ListView</c>-control.</param>
        /// <param name="strText">The text of the new ListViewItem.</param>
        /// <returns>An instance of the new ListViewItem.</returns>
        public static ListViewItem AddItemThreadSafe(this ListView lvwControl,
                                                     string strText)
        {            
            return BaseTools.InvokeSubMethod(lvwControl, 
                                             lvwControl.Items, 
                                             "Add", 
                                             strText) as ListViewItem;
        }

        /// <summary>
        /// Thread-safe way to add an item to a <c>ListView</c>.
        /// </summary>
        /// <param name="lvwControl">The <c>ListView</c>-control.</param>
        /// <param name="litNew">The ListViewItem to add.</param>
        /// <returns>An instance of the new ListViewItem.</returns>
        public static ListViewItem AddItemThreadSafe(this ListView lvwControl,
                                                     ListViewItem litNew)
        {
            return BaseTools.InvokeSubMethod(lvwControl, 
                                             lvwControl.Items, 
                                             "Add", 
                                             litNew) as ListViewItem ;
        }

        /// <summary>
        /// Thread-safe way to call the SuspendLayout-method.
        /// </summary>
        /// <param name="litControl">The <c>ListView</c>-control.</param>
        public static void SuspendLayoutThreadSafe(this ListView litControl)
        {
            BaseTools.InvokeMethod(litControl, 
                                   "SuspendLayout");
        }

        /// <summary>
        /// Thread-safe way to call the ResumeLayout-method.
        /// </summary>
        /// <param name="lvwControl">The <c>ListView</c>-control.</param>
        public static void ResumeLayoutThreadSafe(this ListView lvwControl)
        {
            BaseTools.InvokeMethod(lvwControl, 
                                   "ResumeLayout");
        }

        /// <summary>
        /// Thread-safe way to call the Clear-method.
        /// </summary>
        /// <param name="lvwControl">The <c>ListView</c>-control.</param>
        public static void ClearThreadSafe(this ListView lvwControl)
        {
            BaseTools.InvokeMethod(lvwControl,
                                   "Clear");
        }

        /// <summary>
        /// Thread-safe way to call the Items.Remove-method.
        /// </summary>
        /// <param name="lvwControl">The <c>ListView</c>-control.</param>
        /// <param name="litRemove">The <c>ListViewItem</c> to remove from the control.</param>
        public static void RemoveThreadSafe(this ListView lvwControl,
                                            ListViewItem litRemove)
        {             
            BaseTools.InvokeSubMethod(lvwControl, 
                                      "Items", 
                                      "Remove", 
                                      litRemove);
        }

        #endregion

        #region extensions for ListViewItemCollection

        /// <summary>
        /// Clears all ListViewItems from the Items-collection.
        /// </summary>
        /// <param name="colItems">The Items-collection to clear.</param>
        /// <param name="lvwControl">The <c>ListView</c> which items should be cleared.</param>
        public static void ClearThreadSafe(this ListView.ListViewItemCollection colItems,
                                           ListView lvwControl)
        {
            BaseTools.InvokeSubMethod(lvwControl, 
                                      colItems, 
                                      "Clear");
        }

        /// <summary>
        /// Adds an item to a <c>ListView.</c>
        /// </summary>
        /// <param name="colItems">The Items-collection for adding.</param>
        /// <param name="lvwControl">The <c>ListView</c> control.</param>
        /// <param name="strText">The text of the ListViewItem.</param>
        /// <returns>The instantiated ListViewItem.</returns>
        public static ListViewItem AddThreadSafe(this ListView.ListViewItemCollection colItems,
                                                 ListView lvwControl,
                                                 string strText)
        {
            return BaseTools.InvokeSubMethod(lvwControl, 
                                             colItems, 
                                             "Add", 
                                             strText) as ListViewItem;
        }

        /// <summary>
        /// Adds an item to a <c>ListView.</c>
        /// </summary>
        /// <param name="colItems">The Items-collection for adding.</param>
        /// <param name="lvwControl">The <c>ListView</c> control.</param>
        /// <param name="litNew">The instance of the new ListViewItem.</param>
        /// <returns>The instantiated ListViewItem.</returns>
        public static ListViewItem AddThreadSafe(this ListView.ListViewItemCollection colItems,
                                                 ListView lvwControl,
                                                 ListViewItem litNew)
        {
            BaseTools.InvokeSubMethod(lvwControl, 
                                      colItems, 
                                      "Add", 
                                      litNew);
            return litNew;
        }

        /// <summary>
        /// Removes an item from a <c>ListView.</c>
        /// </summary>
        /// <param name="colItems">The Items-collection where the item should be removed.</param>
        /// <param name="lvwControl">The <c>ListView</c> control.</param>
        /// <param name="litRemove">The ListViewItem which should be removed.</param>        
        public static void RemoveThreadSafe(this ListView.ListViewItemCollection colItems,
                                            ListView lvwControl,
                                            ListViewItem litRemove)
        {
            BaseTools.InvokeSubMethod(lvwControl, 
                                      colItems, 
                                      "Remove", 
                                      litRemove);
        }

        #endregion

    }
}
